home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / icon / window_print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  6.3 KB  |  221 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: window_print.c,v 4.1 88/06/21 14:00:15 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/icon/RCS/window_print.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/icon/RCS/window_print.c,v $$Revision: 4.1 $";
  12.  
  13. /* print any window on the hp think jet (use lpr filter) */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <sys/file.h>
  18. #include "term.h"
  19. #include "dump.h"
  20.  
  21. #define TEMP    "/tmp/pr."            /* temp file name */
  22. #define MESSAGE    "\rwindow dump"            /* window message */
  23. #define WORKING    "\rworking..................."    /* print in progress */
  24. #define JOB    "window"            /* default page header */
  25. #define OPTION    'v'                /* lpr filter option */
  26.  
  27. #define MSG_OK        "ok"            /* message ack - success */
  28. #define MSG_BAD        "x"            /* message nac - failure */
  29. #define MSG_FILE    "F"            /* service type - no data */
  30. #define NAME        "print bitmap"        /* name of service */
  31.  
  32. #define dprintf    if(debug) fprintf
  33.  
  34. #define GET_OPT(i)    \
  35.     strlen(argv[i])>2 ? argv[i]+2 : argv[++i]
  36. #define Min(x)        ((x)<5 ? 5 : (x))
  37.  
  38. char command[100];                /* lpr command to run */
  39. char line[100];                    /* event input buffer */
  40. char temp[20];                    /* temp file name */
  41. char my_host[20];                /* my host */
  42. char mgr_host[20];                /* mgr host */
  43. char title[40];                    /* window title */
  44.  
  45. int remote;                    /* remote from mgr host */
  46. int debug;
  47.  
  48. main(argc,argv)
  49. int argc;
  50. char **argv;
  51.    {
  52.    register int i, c;
  53.    int x,y;                    /* bitmap size */
  54.    int id,sub;                    /* window to dump */
  55.    int clean();
  56.  
  57.    char *printer = NULL;            /* printer name */
  58.    char *job = JOB;                /* job name */
  59.    char *filter = NULL;                /* pre-filter */
  60.    char *message = NULL;            /* message line */
  61.    char option = OPTION;            /* lpr filter */
  62.    char *noprint = NULL;            /* just same file */
  63.  
  64.    ckmgrterm( *argv );
  65.    gethostname(my_host,sizeof(my_host));
  66.    debug = getenv("DEBUG");
  67.  
  68.    /* check arguments */
  69.  
  70.    for(i=1;i<argc;i++) {
  71.       if (*argv[i] == '-')
  72.          switch (argv[i][1]) {
  73.             case 'f':                /* specify filter */
  74.                filter = GET_OPT(i);
  75.                break;
  76.             case 'p':                /* printer name */
  77.             case 'P':                /* printer name */
  78.                printer = GET_OPT(i);
  79.                break; 
  80.             case 'm':                /* message line */
  81.                message = GET_OPT(i);
  82.                break; 
  83.             case 'j':                /* job name */
  84.             case 'J':                /* job name */
  85.                job = GET_OPT(i);
  86.                break; 
  87.             case 'o':                /* option flag */
  88.                option = *(GET_OPT(i));
  89.                break; 
  90.             case 'x':                /* dont print name */
  91.                noprint = GET_OPT(i);
  92.                break; 
  93.             default:
  94.                fprintf(stderr,"%s: invalid flag %c ignored\n",argv[0],argv[i][1]);
  95.             }
  96.       else
  97.          fprintf(stderr,"%s: invalid argument %s ignored\n",argv[0],argv[i]);
  98.       }
  99.  
  100.    /* setup mgr */
  101.  
  102.    m_setup(M_FLUSH);
  103.    m_push(P_FLAGS|P_EVENT);
  104.    m_ttyset();
  105.  
  106.    get_param(mgr_host,0,0,0);
  107.    remote = strcmp(mgr_host,my_host);
  108.    dprintf(stderr,"my host (%s), mgr host (%s)\n",my_host,mgr_host);
  109.  
  110.    if (message)
  111.       sprintf(title,"\r%s",message);
  112.    else if (printer && remote)
  113.       sprintf(title,"%s at %s on %s",MESSAGE,my_host,printer);
  114.    else if (remote)
  115.       sprintf(title,"%s at %s",MESSAGE,my_host);
  116.    else if (printer)
  117.       sprintf(title,"%s on %s",MESSAGE,printer);
  118.    else
  119.       strcpy(title,MESSAGE);
  120.  
  121.    get_size(&x,&y,0,0);
  122.    if (!debug)
  123.       m_sizeall(x,y,Min(strlen(title)-1),1);
  124.    m_setmode(M_NOWRAP);
  125.    m_clear();
  126.    m_printstr(title);
  127.    m_clearmode(M_ACTIVATE);
  128.    
  129.    m_setevent(ACTIVATED,"A\r");            /* window made active */
  130.    m_setevent(DEACTIVATED,"D\r");        /* window made in-avtive */
  131.    m_setevent(BUTTON_1,"B %w\r");        /* button 1 hit */
  132.    m_setevent(REDRAW,"R\r");
  133.    m_setevent(RESHAPE,"S\r");
  134.    
  135.    signal(SIGHUP,clean);
  136.    signal(SIGINT,clean);
  137.    signal(SIGTERM,clean);
  138.  
  139.    /* build command */
  140.  
  141.    sprintf(temp,"%s%s%d",TEMP,my_host,getpid());
  142.    dprintf(stderr,"temp file name: (%s)\n",temp);
  143.    
  144.    if (noprint) {
  145.       if (remote)
  146.          sprintf(command,"rcp %s:%s %s",mgr_host,temp,noprint);
  147.       else
  148.          sprintf(command,"cp %s %s",temp,noprint);
  149.       }
  150.    else {
  151.       if (remote)
  152.          sprintf(command,"rsh %s cat %s | ",mgr_host,temp);
  153.       else
  154.          sprintf(command,"< %s ",temp);
  155.       if (filter) 
  156.          sprintf(command+strlen(command),"%s | ",filter);
  157.       strcat(command,"lpr ");
  158.       if (printer)
  159.          sprintf(command+strlen(command),"-P%s ",printer);
  160.       sprintf(command+strlen(command),"-J%s -%c",job,option);
  161.       }
  162.    dprintf(stderr,"command: (%s)\n",command);
  163.    
  164.    while (m_gets(line) != NULL) {
  165.      dprintf(stderr,"main loop got: %s",line);
  166.      switch(c = *line) {
  167.         case 'S':                /* window reshaped */
  168.            get_size(&x,&y,0,0);
  169.            if (!debug)
  170.               m_sizeall(x,y,strlen(title)-1,1);
  171.            m_clear();
  172.            m_printstr(title);
  173.            break;
  174.         case 'R':                /* window redrawn */
  175.            m_printstr(title);
  176.            break;
  177.         case 'A':                /* window activated */
  178.            m_setmode(M_WOB);
  179.            break;
  180.         case 'D':                /* window deactivated */
  181.            m_clearmode(M_WOB);
  182.            break;
  183.         case 'B':                /* button hit */
  184.            id = 0;
  185.            sscanf(line+2,"%d.%d",&id,&sub);
  186.            dprintf(stderr," got: %d, %d\n",id,sub);
  187.            if (id) {
  188.               m_othersave(id,sub,temp);
  189.               m_clearmode(M_WOB);
  190.               m_printstr(WORKING);
  191.               sleep(1); 
  192.               system(command,temp);
  193.               if (remote) {
  194.                  sprintf(command,"rsh %s rm %s\n",mgr_host,temp);
  195.                  system(command);
  196.                         }
  197.               else
  198.                  unlink(temp);
  199.               }
  200.            m_printstr(title);
  201.            m_clearmode(M_ACTIVATE);
  202.            break;  
  203.         }
  204.       }
  205.    }
  206.  
  207. clean(n)
  208. int n;
  209.    {
  210.    m_ttyreset();
  211.    if (remote) {
  212.       sprintf(command,"rsh %s rm %s\n",mgr_host,temp);
  213.       system(command);
  214.         }
  215.    else
  216.       unlink(temp);
  217.    m_pop();
  218.    m_clear();
  219.    exit(n);
  220.    }
  221.